<?php
/**
 * 天酷 建站 框架 access数据库类
 */
defined('PATH') or exit('Access Denied');
class db_access {
	var $querynum = 0;
	var $conn;
	var $insertid = 0;
	var $cursor = 0;
	var $pre;

	function connect($dbhost, $dbuser = '', $dbpw = '', $dbname = '', $pconnect = 0) {
		$this->conn = new com('adodb.connection') or exit('Cannot start ADO');
		try {
			$this->conn->open("DRIVER={Microsoft Access Driver (*.mdb)};dbq=$dbhost;uid=$dbuser;pwd=$dbpw");
			if($this->conn->state == 0) {
				$this->conn->open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=$dbhost");
				if($this->conn->state == 0)	exit('Can not connect to Access Database !');
			}
		} catch (Exception $e) {
			echo iconv("GBK", "UTF-8",  $e->getMessage());
			exit();
		}

		define('NUM', 1);
		define('ASSOC', 2);
		define('BOTH', 3);
	}

	function query($sql, $type = '', $expires = 3600, $dbname = '') {
		if($this->pre)$sql=str_replace("@#__", $this->pre, $sql);
		$this->querynum++;
		$sql = trim($sql);
		if (C('TK_DEBUG'))Log::Write($sql, 'sql');
		if(preg_match("/^(select.*)limit ([0-9]+)(,([0-9]+))?$/i", $sql, $matchs)) {
			$sql = $matchs[1];
			$offset = $matchs[2];
			$pagesize = $matchs[4];
			try {
				$query = $this->conn->Execute($sql);
				return $this->limit($query, $offset, $pagesize);
			} catch (Exception $e) {
				echo iconv("GBK", "UTF-8", $this->errormsg());
			}
		} else {
			$sql=iconv("UTF-8", "GBK", $sql);
			try {
				return $this->conn->Execute($sql);
			} catch (Exception $e) {
				echo iconv("GBK", "UTF-8", $this->errormsg());
			}
		}
	}

	function get_one($query) {
		if($this->pre)$query=str_replace("@#__", $this->pre, $query);
		$this->querynum++;
		if (C('TK_DEBUG'))Log::Write($query, 'sql');
		try {
			$rs = $this->conn->Execute($query);
			$r = $this->fetch_array($rs);
			$this->free_result($rs);
			return $r;
		} catch (Exception $e) {
			echo iconv("GBK", "UTF-8", $this->errormsg());
		}
	}

	function fetch_array($rs, $result_type = 3) {
		if(is_array($rs)) {
			return $this->cursor < count($rs) ? $rs[$this->cursor++] : FALSE;
		} else {
			if($rs->EOF) return FALSE;
			$array = array();
			for($i = 0; $i < $this->num_fields($rs); $i++) {
				$fielddata = $rs->Fields[$i]->Value;
				if($result_type == NUM || $result_type == BOTH) $array[$i] = iconv("GBK", "UTF-8", $fielddata);
				if($result_type == ASSOC || $result_type == BOTH) $array[$rs->Fields[$i]->Name] = iconv("GBK", "UTF-8", $fielddata);
			}
			$rs->MoveNext();
			return $array;
		}
	}
	function fetchRows($sql){
		$query=$this->query($sql);
		while ($row=$this->fetch_array($query)) {
			$data[]=$row;
		}
		if (isset($data))return $data ;else return false;
	}
	function affected_rows($rs) {
		return count($rs);
	}

	function num_rows($rs) {
		return is_array($rs) ? count($rs) : $rs->recordcount;
	}

	function num_fields($rs) {
		return $rs->Fields->Count;
	}

	function fetch_assoc($rs) {
		return $this->fetch_array($rs, ASSOC);
	}

	function fetch_row($rs) {
		return $this->fetch_array($rs, NUM);
	}

	function free_result($rs) {
		if(is_resource($rs)) $rs->close();
	}

	function error() {
		return $this->conn->Errors[$this->conn->Errors->Count-1]->Number;
	}

	function errormsg() {
		return $this->conn->Errors[$this->conn->Errors->Count-1]->Description;
	}

	function close() {
		$this->conn->close();
	}

	function limit($rs, $offset, $pagesize = 0) {
		if($pagesize > 0) {
			$rs->Move($offset);
		} else {
			$pagesize = $offset;
		}
		$info = array();
		for($i = 0; $i < $pagesize; $i++) {
			$r = $this->fetch_array($rs);
			if(!$r) break;
			$info[] = $r;
		}
		$this->free_result($rs);
		$this->cursor = 0;
		return $info;
	}
}
?>